var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        menu: {
            parentName: null,
            parentId: 0,
            type: 1,
            orderNum: 0,
            isHide:0
        },
        queryData: {
            keyword: null
        }
    },
    methods: {
        getMenu: function (menuId) {
            //加载菜单树
            $.get((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/menu/select", function (r) {
                ztree = $.fn.zTree.init($("#menuTree"), setting, r.menuList);
                var node = ztree.getNodeByParam("id", vm.menu.parentId);
                ztree.selectNode(node);

                vm.menu.parentName = node.name;
            })
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            //获取select父级，自动赋值，减少不必要操作
            let selected = $('#menuTable').bootstrapTreeTable('getSelections');
            let parentId = 0;
            if (null != selected) {
                parentId = selected[0].id;
            }
            vm.menu = {
                parentName: null,
                parentId: parentId,
                type: 1,
                orderNum: 0,
                icon: "fa fa-circle-o",
                isHide: 0
            };
            vm.getMenu();
        },
        update: function () {
            var menuId = getMenuId();
            if (menuId == null) {
                return;
            }

            $.get((window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/menu/info/" + menuId, function (r) {
                vm.showList = false;
                vm.title = "修改";
                vm.menu = r.menu;

                if (!com.isNull(vm.menu.icon)) {
                    $("#menuIcon").attr("class", vm.menu.icon);
                }

                vm.getMenu();
            });
        },
        del: function () {
            var menuId = getMenuId();
            if (menuId == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/menu/delete",
                    data: "menuId=" + menuId,
                    success: function (r) {
                        if (r.code === 0) {
                            toast('操作成功');
                            vm.reload();
                        } else {
                            warning(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if (vm.validator()) {
                return;
            }

            var url = vm.menu.id == null ? "sys/menu/save" : "sys/menu/update";
            $.ajax({
                type: "POST",
                url: (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + url,
                contentType: "application/json",
                data: JSON.stringify(vm.menu),
                success: function (r) {
                    if (r.code === 0) {
                        toast('操作成功');
                        vm.reload();
                    } else {
                        warning(r.msg);
                    }
                }
            });
        },
        menuTree: function () {
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择菜单",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#menuLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.menu.parentId = node[0].id;
                    vm.menu.parentName = node[0].name;

                    layer.close(index);
                }
            });
        },
        reload: function () {
            vm.showList = true;

            Menu.table.refresh({query: vm.queryData});
        },
        validator: function () {
            if (isBlank(vm.menu.name)) {
                warning("菜单名称不能为空");
                return true;
            }

            //菜单
            if (vm.menu.type === 1 && isBlank(vm.menu.url)) {
                warning("菜单URL不能为空");
                return true;
            }
        }
    }
});


function selectIcons() {
    var url = (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "modules/sys/icons.html";
    var win = layer.open({
        type: 2,
        maxmin: true,
        shadeClose: true,
        skin: 'layui-layer-molv',
        title: '图标选择',
        area: ["80%", "80%"],
        content: url,
        success: function (layero, index) {
            var info = '<font color="red" class="pull-left mt10">提示：双击选择图标。</font>';
            layero.find('.layui-layer-btn').append(info);

            var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var winc = iframeWin.window || iframeWin.contentWindow;
            winc.funIcon = function () {
                $(".layui-layer-btn0").click()
            }
        },
        btn: ['<i class="fa fa-close"></i> 关闭',
            '<i class="fa fa-eraser"></i> 清除'],
        btn1: function (index, layero) {
            var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
            var winc = iframeWin.window || iframeWin.contentWindow;
            var icon = winc.$("#icon").val();
            $("#menuIcon").attr("class", icon);
            vm.menu.icon = icon;
            layer.close(index);
        },
        btn2: function (index, layero) {
            var icon = "fa fa-circle-o";
            vm.menu.icon = icon;

            $("#menuIcon").attr("class", icon);
        }
    });
}

var Menu = {
    id: "menuTable",
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Menu.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '菜单ID', field: 'id', visible: false, align: 'center', valign: 'middle', width: '80px'},
        {title: '菜单名称', field: 'name', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '上级菜单', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {
            title: '图标',
            field: 'icon',
            align: 'center',
            valign: 'middle',
            sortable: true,
            width: '80px',
            formatter: function (item, index) {
                return item.icon == null ? '' : '<i class="' + item.icon + ' fa-lg"></i>';
            }
        },
        {
            title: '类型',
            field: 'type',
            align: 'center',
            valign: 'middle',
            sortable: true,
            width: '100px',
            formatter: function (item, index) {
                if (item.type === 0) {
                    return '<span class="label label-primary">目录</span>';
                }
                if (item.type === 1) {
                    return '<span class="label label-success">菜单</span>';
                }
                if (item.type === 2) {
                    return '<span class="label label-warning">按钮</span>';
                }
            }
        },
        {title: '排序号', field: 'orderNum', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '菜单URL', field: 'url', align: 'center', valign: 'middle', sortable: true, width: '160px'},
        {title: '授权标识', field: 'perms', align: 'center', valign: 'middle', sortable: true}]
    return columns;
};


function getMenuId() {
    var selected = $('#menuTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        toast("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}

var table;
$(function () {
    var colunms = Menu.initColumn();
    table = new TreeTable(Menu.id, (window.localStorage.getItem("baseURL")?window.localStorage.getItem("baseURL"):"") + "sys/menu/list", colunms);
    table.setExpandColumn(2);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("parentId");
    table.setExpandAll(false);
    table.setHeight("100%");
    table.init();
    Menu.table = table;
});
